package com.cos.hospital.tool.takephoto;

import android.Manifest;
import android.content.Intent;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.tool.MyUtil;
import com.cos.hospital.tool.ULog;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;

import java.io.File;
import java.util.List;

import io.reactivex.disposables.Disposable;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;


/**
 * 继承这个类来让Activity获取选择图片和拍照的能力
 */
public abstract class GetPhotoActivity extends BaseActivity {
    private static final int REQUEST_CODE_CHOOSE = 66;
    private static final int REQUEST_CODE_CAMERA = 68;
    private static final int REQUEST_CODE_PREVIEW = 88;
    private String targerFilePath;

    protected abstract void getPhotoSuccess(File file);

    protected void openAlbum() {
        Matisse.from(this)
            .choose(MimeType.of(MimeType.JPEG, MimeType.PNG))
            .maxSelectable(1)
            .showSingleMediaType(true)
            .imageEngine(new Glide4Engine())
            .theme(R.style.Matisse_Dracula)
            .forResult(REQUEST_CODE_CHOOSE);
    }

    protected void openCamrea() {
        RxPermissions rxPermissions = new RxPermissions(this);
        if (!rxPermissions.isGranted(Manifest.permission.CAMERA)) {
            Disposable subscribe = rxPermissions.request(Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) {
                        takePhoto();
                    } else {
                        MyUtil.getInstance().toSetting(context, "你已拒绝获取打开相机权限，为保证你能正常拍照，请开启该权限。", 10, false);
                    }
                });
            addEvent(subscribe);
        } else {
            takePhoto();
        }
    }

    private void takePhoto() {
        startActivityForResult(new Intent(this, CameraActivity.class), REQUEST_CODE_CAMERA);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                //选择图片
                case REQUEST_CODE_CHOOSE:
                    if (data == null) {
                        return;
                    }
                    List<String> images = Matisse.obtainPathResult(data);
                    if (images != null && images.size() != 0) {
                        String filePath = images.get(0);
                        Intent intent = new Intent(this, PreviewActivity.class);
                        intent.putExtra(Code.SP.IMAGEPATH, filePath);
                        startActivityForResult(intent, REQUEST_CODE_PREVIEW);
                    }
                    break;
                //拍照
                case REQUEST_CODE_CAMERA:
                    if (data == null) {
                        return;
                    }
                    targerFilePath = data.getStringExtra(Code.SP.SAVEPATH);
                    compress();
                    break;
                //预览
                case REQUEST_CODE_PREVIEW:
                    if (data == null) {
                        return;
                    }
                    targerFilePath = data.getStringExtra(Code.SP.SAVEPATH);
                    compress();
                    break;
            }
        }
    }

    private void compress() {
        Luban.with(this)
            .load(new File(targerFilePath))
            .ignoreBy(100)
            .setCompressListener(new OnCompressListener() { //设置回调
                @Override
                public void onStart() {
                }

                @Override
                public void onSuccess(File file) {
                    getPhotoSuccess(file);
                }

                @Override
                public void onError(Throwable e) {
                    ULog.d(e.toString());
                }
            }).launch();
    }
}
